package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/1/18
 * Time:18:16
 **/
public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入删除图书名字：");
        String name = scanner.nextLine();
        int i = 0;
        for (; i < bookList.getSize(); i++) {
            Book book = bookList.getBooks(i);
            if(book.getName().equals(name)){
                //找到此书了
                break;
            }
        }
        if(i >= bookList.getSize()) {
            System.out.println("未找到！");
            return;
        }
        int pos =i;
        for (int j = pos; j < bookList.getSize()-1; j++) {
            Book book = bookList.getBooks(j+1);
            bookList.setBooks(j,book);
        }
        System.out.println("已删除");
    }
}
